package app.thread;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

/**
 * 扫描登录后台管理线程
 * @author zoe
 *
 */
@Service
public class ScanLoginThread implements Runnable {
	
	private static Logger logger = Logger.getLogger(ScanLoginThread.class);
	
	public Map<String,Object> waitScanQrMap;
	
	private String key;
	
	private Object value;
	
	public ScanLoginThread(){
		waitScanQrMap = new HashMap<String, Object>();
	}
	
	public Map<String, Object> getWaitScanQrMap() {
		return waitScanQrMap;
	}

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}
	
	public void setValue(String key,Object value) {
		if(waitScanQrMap.containsKey(key)){
			waitScanQrMap.put(key, value);
			logger.info("设置登录值"+key+":"+value);
		}
	}

	public void setKey(String key) {
		this.key = key;
		waitScanQrMap.put(key, null);
		logger.info("新建登录键"+key);
	}

	@Override
	public void run() {
		while(true){
			try {
				Thread.sleep(1000);
				Object waitScanQrValue = waitScanQrMap.get(key);
				if(waitScanQrValue==null){
					setValue(null);
				}else{
					setValue(waitScanQrValue);
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	
	public Object getWaitScanQrMap(String key) {
		Object object = waitScanQrMap.get(key);
		logger.info("获取登录值"+key+":"+object);
		return object;
	}

}
